import net from '@/network/net.js';
import $ from 'jquery';
import store from '@/store/store.js';

let api = {
    'login': '/app/account/v1/login',
    'checkToken': '/app/account/v1/checkToken',
    'signOut': '/app/account/v1/signOut',
    'getAccList': '/app/account/v1/getAccList',
    'saveAcc': '/app/account/v1/saveAcc',
    'delAcc': '/app/account/v1/delAcc',
    'enableAcc': '/app/account/v1/enableAcc',
};

let postCfg = {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    transformRequest: function(data) {
        return $.param(data)
    }
}

let res = {

};

function login(account, password, code, imgToken) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.login, {
            account: account, 
            password: password, 
            code: code, 
            imgToken: imgToken
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function checkToken() {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.checkToken, {
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function signOut() {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.signOut, {
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function getAccList(page, pageSize) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.getAccList, {
            page: page,
            pageSize: pageSize,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function saveAcc(account, password) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.saveAcc, {
            account: account,
            password: password,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function delAcc(account) {
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.delAcc, {
            account: account,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

function enableAcc(account,enable){
    let p = new Promise(function(resolve, reject) {
        net.req.post(api.enableAcc, {
            account: account,
            enable: enable
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}

export default {
    login,
    checkToken,
    signOut,
    getAccList,
    saveAcc,
    delAcc,
    enableAcc,
}
